﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace DevicesInt
{
    public interface IDeviceInterface
    {
        DeviceControl GetController(string ip);
        string Getstatus(string ip);
        string Getparams(string ip);
        string Getlog(string ip, string logFilePath);
        string TrapHandle(string devName, byte[] serializedTrap, DateTime date);
        //string TrapHandle();
    }

    public abstract class DeviceUnmanagedCall : IDeviceInterface
    {
        [System.Reflection.Obfuscation(Feature = "DllExport")]
        public string CallMethod(string func, params string[] pars)
        {
            string ret;
            switch (func)
            {
                case "getstatus":
                    try
                    {
                        ret = Getstatus(pars[0]);
                        return ret;
                    }
                    catch (Exception ex)
                    {
                        if (ex is IndexOutOfRangeException)
                            return "err:" + Errors.WRONG_INPUT_DATA + Errors.ARGUMENTS_COUNT;
                        return "err:" + ex.Message;
                    }
                case "getparams":
                    try
                    {
                        ret = Getparams(pars[0]);
                        return ret;
                    }
                    catch (Exception ex)
                    {
                        if (ex is IndexOutOfRangeException)
                            return "err:" + Errors.WRONG_INPUT_DATA + Errors.ARGUMENTS_COUNT;
                        return "err:" + ex.Message;
                    }
                case "traphandle":
                    try
                    {
                        ret = TrapHandle("", new byte[0], DateTime.Now);
                        return ret;
                    }
                    catch (Exception ex)
                    {
                        return "err:" + ex.Message;
                    }
                case "getlog":
                    try
                    {
                        ret = Getlog(pars[0], pars[1]);
                        return ret;
                    }
                    catch (Exception ex)
                    {
                        if (ex is IndexOutOfRangeException)
                            return "err:" + Errors.WRONG_INPUT_DATA + Errors.ARGUMENTS_COUNT;
                        return "err:" + ex.Message;
                    }
                //case "setparams":
                //    try
                //    {
                //        ret = Setparams(pars[0], pars[1]);
                //        return ret;
                //    }
                //    catch (Exception ex)
                //    {
                //        if (ex is IndexOutOfRangeException)
                //            return "err:" + Errors.WRONG_INPUT_DATA + Errors.ARGUMENTS_COUNT;
                //        return "err:" + ex.Message;
                //    }
                case "flush":
                    try
                    {
                        ret = Flush();
                        return ret;
                    }
                    catch (Exception ex)
                    {
                        return "err:" + ex.Message;
                    }
                default:
                    return "err:Такой функции не существует";
            }
        }

        public abstract DeviceControl GetController(string ip);
        public abstract string Getstatus(string ip);
        public abstract string Getparams(string ip);
        public abstract string Getlog(string ip, string logFilePath);
        public abstract string TrapHandle(string devName, byte[] serializedTrap, DateTime date);
        public abstract string Flush();
    }

    public abstract class DeviceControl
    {
        public static CultureInfo CurrentCulture { get; set; }

        public enum ControlType
        {
            NetworkLink,
            Window,
            Control,
            ParametersList,
            None
        }

        public class ParamInfo
        {
            public struct ParamType
            {
// ReSharper disable InconsistentNaming
                private const string STRING = "string";
                private const string INT = "int";
                private const string DOUBLE = "double";
                private const string DATETIME = "datetime";
// ReSharper restore InconsistentNaming
            }

            public ParamInfo()
            {
                CanSet = false;
            }

            public string Name { get; set; }
            public string Value { get; set; }
            public string Type { get; set; }
            public string StringFormat { get; set; }
            public bool CanSet { get; set; }
        }

        public abstract ControlType GetControlType { get; }

        public abstract object GetControl(CultureInfo cultureInfo = null);

        public abstract string ShowControl();

        public abstract List<ParamInfo> GetParametersList(); 

        public abstract string GetParametersString();

        public abstract string SetParameter(string paramValue, string paramName = null);
    }

    
}
